#pragma once

#include "../Common/Common.h"
#include "pthread/pthread.h"
#include "pthread/semaphore.h"

//-----------------------------------
// PosixThread
//-----------------------------------
class PSDF_CORE_DLL_DECL PosixThread
{
public:
	PosixThread( void );
	
	virtual ~PosixThread( void );

public:
	void				start();
	void				stop();
	pthread_t			getThreadId();

	static void			waitUntilDone(pthread_t threadId);
	static void			msleep( long millisec );

protected:
	virtual void		run() = 0;

private:
	static void *		threadFunc( void * instance );

private:
	pthread_t			_threadId;
};

//-----------------------------------
// PosixMutex
//-----------------------------------
class PSDF_CORE_DLL_DECL PosixMutex
{
public:
	PosixMutex();
	~PosixMutex();

public:
	inline void			lock();
	inline void			unlock();
	inline bool			tryLock();

private:
	pthread_mutex_t		_mutex;	
};


//-----------------------------------
// PosixSemaphore
//-----------------------------------
class PSDF_CORE_DLL_DECL PosixSemaphore
{
public:
	explicit			PosixSemaphore( int initCount = 0 );
						~PosixSemaphore();

public:
	inline void			acquire();
	inline void			release( unsigned int n = 1 );
	inline bool			tryAcquire();

private:
	sem_t				_sem;
};

//-------------------------------------
// PosixReadWriteLock
//-------------------------------------
class PSDF_CORE_DLL_DECL PosixReadWriteLock
{
public:
	PosixReadWriteLock();
	~PosixReadWriteLock();

public:
	inline void			lockForRead();
	inline void			lockForWrite();
	inline bool			tryLockForRead();
	inline bool			tryLockForWrite();
	inline void			unlock();

private:
	pthread_rwlock_t	_rwlock;
};